﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using CMN.Models;
using CMN.Models.Interfaces;
using CMN.Entity;
using System.Web.Routing;
using CMN.Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using CMN.Controllers.Filters;
namespace CMN.Controllers
{
    [ExceptionFilter]
    [LoggerFilter]
    [UserAuthorize]
    public class Sys_ModuleExtPermissionController : Controller
    {

        // 返回json字符串
        protected string returnJson = string.Empty;

        /// <summary>
        /// 转到View页面
        /// </summary>
        /// <returns></returns>
        public ViewResult Index()
        {
            return View("Index");//页面地址
        }

        /// <summary>
        /// 返回模块扩展权限列表
        /// </summary>
        /// <returns>json</returns>
        public string List(string actionId)
        {
            //返回json数组
            int moduleId = Utils.StringToInt(actionId,0);

            int pageSize = (int)Utils.GetQueryAndFormString("limit", this, 4, 1,DataType.Int);
            int pageIndex = (int)Utils.GetQueryAndFormString("start", this, 9, 1,DataType.Int);
            QueryParam qp = new QueryParam();
            qp.PageIndex = pageIndex / pageSize + 1; 
            qp.PageSize = pageSize;
            qp.Where = "ModuleID=" + moduleId.ToString();
            int recordNum = 0;
            IDataService<Sys_ModuleExtPermissionEntity> Sys_ModuleExtPermissionSv= ServiceBuilder.BuildSys_ModuleExtPermissionService();
            List<Sys_ModuleExtPermissionEntity> sys_moduleextpermissionList =Sys_ModuleExtPermissionSv.List(qp, out recordNum);
            returnJson = string.Concat("{totalPorperty:", recordNum, ",root:",JsonConvert.SerializeObject(sys_moduleextpermissionList), "}");
            return returnJson;

        }

        /// <summary>
        /// 查看模块扩展权限 
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
        public string Look(string actionId)
        {
            IDataService<Sys_ModuleExtPermissionEntity> Sys_ModuleExtPermissionSv = ServiceBuilder.BuildSys_ModuleExtPermissionService();
            int intId = Utils.StringToInt(actionId,0);
            Sys_ModuleExtPermissionEntity sys_moduleextpermission = Sys_ModuleExtPermissionSv.Look(intId);
            if (sys_moduleextpermission != null)
                returnJson = string.Concat("{success:true,data:", JsonConvert.SerializeObject(sys_moduleextpermission), "}");
            else
                returnJson = "{success:'false'}";
            return returnJson;
        }

        /// <summary>
        /// 更新模块扩展权限
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
        public string Update(string actionId)
        {
            int intId = Utils.StringToInt(actionId,0);
            if (intId != 0)
            {
                Sys_ModuleExtPermissionEntity sys_moduleextpermission = new Sys_ModuleExtPermissionEntity();
                sys_moduleextpermission.DataTable_Action = DataTableAction.Update;
                sys_moduleextpermission.ExtPermissionID = intId;
                sys_moduleextpermission.ModuleID = (Int32)Utils.GetQueryAndFormString("ModuleID", this, 10, 1, DataType.Int);
                sys_moduleextpermission.PermissionName = (String)Utils.GetQueryAndFormString("PermissionName", this, 100, 1, DataType.Str);
                sys_moduleextpermission.PermissionValue = (Int32)Utils.GetQueryAndFormString("PermissionValue", this, 10, 1, DataType.Int);
                sys_moduleextpermission.PermissionCode = (String)Utils.GetQueryAndFormString("PermissionCode", this, 100, 1, DataType.Str);
                IDataService<Sys_ModuleExtPermissionEntity> Sys_ModuleExtPermissionSv = ServiceBuilder.BuildSys_ModuleExtPermissionService();
                if (Sys_ModuleExtPermissionSv.Update(sys_moduleextpermission) == "")
                {
                    returnJson = "{success:'true'}";
                }
                else
                    returnJson = "{success:'false'}";
            }
            else
            {
                returnJson = "{success:'false'}";
            }

            return returnJson;
        }

        /// <summary>
        /// 新增模块扩展权限
        /// </summary>
        /// <returns>json</returns>
        public string Insert()
        {
            Sys_ModuleExtPermissionEntity sys_moduleextpermission = new Sys_ModuleExtPermissionEntity();
            sys_moduleextpermission.DataTable_Action = DataTableAction.Insert;
            sys_moduleextpermission.ModuleID = (Int32)Utils.GetQueryAndFormString("ModuleID", this, 10, 1, DataType.Int);
            sys_moduleextpermission.PermissionName = (String)Utils.GetQueryAndFormString("PermissionName", this, 100, 1, DataType.Str);
            sys_moduleextpermission.PermissionValue = (Int32)Utils.GetQueryAndFormString("PermissionValue", this, 10, 1, DataType.Int);
            sys_moduleextpermission.PermissionCode = (String)Utils.GetQueryAndFormString("PermissionCode", this, 100, 1, DataType.Str);
            IDataService<Sys_ModuleExtPermissionEntity> Sys_ModuleExtPermissionSv = ServiceBuilder.BuildSys_ModuleExtPermissionService();
            if (Sys_ModuleExtPermissionSv.Insert(sys_moduleextpermission) == "")
            {
                returnJson = "{success:'true'}";
            }
            else
                returnJson = "{success:'false'}";
            return returnJson;
        }

        /// <summary>
        /// 删除模块扩展权限
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
        public string Delete(string actionId)
        {
            int intId = Utils.StringToInt(actionId,0);
            if (intId != 0)
            {
                Sys_ModuleExtPermissionEntity sys_moduleextpermission = new Sys_ModuleExtPermissionEntity();
                sys_moduleextpermission.ExtPermissionID = intId;
                IDataService<Sys_ModuleExtPermissionEntity> Sys_ModuleExtPermissionSv = ServiceBuilder.BuildSys_ModuleExtPermissionService();
                if (Sys_ModuleExtPermissionSv.Delete(sys_moduleextpermission) == "")
                {
                    returnJson = "{success:'true'}";
                }
                else
                    returnJson = "{success:'false'}";
            }
            else
            {
                returnJson = "{success:'false'}";
            }
            return returnJson;
        }

        /// <summary>
        /// 批量删除模块扩展权限
        /// </summary>
        /// <param name="idList">id列表 234_4_5</param>
        /// <returns>json</returns>
        public string DeleteList(string actionId)
        {
            Common.RegExpression.NumAndBarRegExpression regex = new CMN.Common.RegExpression.NumAndBarRegExpression();
            if (regex.IsMatch(actionId))
            {

                IDataService<Sys_ModuleExtPermissionEntity> Sys_ModuleExtPermissionSv = ServiceBuilder.BuildSys_ModuleExtPermissionService();
                if (Sys_ModuleExtPermissionSv.DeleteList(actionId) == "")
                {
                    returnJson = "{success:'true'}";
                }
                else
                    returnJson = "{success:'false'}";

            }
            else
            {
                returnJson = "{success:'false',message:'参数类型不正确'}";
            }
            return returnJson;
        }

    }
}
